<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Metrika extends Controller_Template {

    public $template = 'main';
    private $project_id;
    private $username;

    public function before() {
        parent::before();
        if (!Auth::instance()->logged_in() AND Request::current()->uri() !== 'login') {
            $this->request->redirect('/login');
        }
        $this->username = Auth::instance()->get_user()->username;
        $model = ORM::factory('project')->where("domain", "=", $this->username)->find();
        $this->project_id = $model->project_id;
    }

    public function action_traffic() {
        $interval = date("Ymd", $this->get_interval());
        $metrika = new Model_Metrika($this->username);
        $traffic_stat = $metrika->get_traffic_stat($interval, date("Ymd"));
        $data['traffic_stat'] = $traffic_stat;
        $this->template->project_id = $this->project_id;
        $this->template->title = "Отчет Посещаемость";
        $this->template->content = View::factory('traffic', $data);
    }

    public function action_sources_summary() {
        $interval = date("Ymd", $this->get_interval());
        $metrika = new Model_Metrika($this->username);
        $summary_stat = $metrika->get_summary_stat($interval, date("Ymd"));
        $data['summary_stat'] = $summary_stat;
        $this->template->project_id = $this->project_id;
        $this->template->title = "источники трафика: сводка";
        $this->template->content = View::factory('sources_summary', $data);
    }
    
    public function action_sources_engines() {
        $interval = date("Ymd", $this->get_interval());
        $metrika = new Model_Metrika($this->username);
        $sources_stat = $metrika->get_sources_engines_stat($interval, date("Ymd"));
        $data['sources_stat'] = $sources_stat;
        $this->template->project_id = $this->project_id;
        $this->template->title = "Источники трафика по поисковым системам";
        $this->template->content = View::factory('sources_engines', $data);
    }

    public function action_sources_phrases() {
        $interval = date("Ymd", $this->get_interval());
        $metrika = new Model_Metrika($this->username);
        $sources_stat = $metrika->get_sources_phrases_stat($interval, date("Ymd"));
        $data['sources_stat'] = $sources_stat;
        $this->template->project_id = $this->project_id;
        $this->template->title = "Источники трафика по поисковым фразам";
        $this->template->content = View::factory('sources_phrases', $data);
    }

    public function action_geo() {
        $interval = date("Ymd", $this->get_interval());
        $metrika = new Model_Metrika($this->username);
        $geo_stat = $metrika->get_geo_stat($interval, date("Ymd"));
        $data['geo_stat'] = $geo_stat;
        $this->template->project_id = $this->project_id;
        $this->template->title = "Посетители по странам мира";
        $this->template->content = View::factory('geo', $data);
    }

    public function action_content() {
        $interval = date("Ymd", $this->get_interval());
        $metrika = new Model_Metrika($this->username);
        $content_stat = $metrika->get_content_stat($interval, date("Ymd"));
        $data['content_stat'] = $content_stat;
        $this->template->project_id = $this->project_id;
        $this->template->title = "Популярное содержание";
        $this->template->content = View::factory('content', $data);
    }

    private function get_interval() {
        $interval = Arr::get($_POST, 'list', 'За месяц');
        switch ($interval) {
            case 'За день':
                $interval = strtotime('now');
                break;
            case 'За неделю':
                $interval = strtotime('-1 week');
                break;
            case 'За месяц':
                $interval = strtotime('-1 month');
                break;
            case 'За квартал':
                $interval = strtotime('-3 month');
                break;
            case 'За пол года':
                $interval = strtotime('-6 month');
                break;
            case 'За год':
                $interval = strtotime('-1 year');
                break;
            default:
                $interval = strtotime('-1 week');
                break;
        }
        return $interval;
    }

}